-- 冲锋
local chongfeng = fk.CreateSkill {
    name = "mcdh_chongfeng",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

----抄的仁区
---@param room Room @ 房间
---@return integer[]
local function GetMCGPile(room)
    room.tag["mcdh_mcg"] = room.tag["mcdh_mcg"] or {}
    return table.simpleClone(room.tag["mcdh_mcg"])
end

local function NotifyMCGPile(room)
    room:sendLog{
        type = "#NotifyMCGPile",
        arg = #GetMCGPile(room),
        card = GetMCGPile(room),
    }

    room:setBanner("@$mcdh_MCGPile", table.simpleClone(room.tag["mcdh_mcg"]))
end

chongfeng:addEffect('viewas',{
    pattern = ".",
    expand_pile = function () Fk:currentRoom():getBanner("@$mcdh_MCGPile") end,
    card_filter = function(self, player,to_select, selected)
        if #selected == 0 then
            local ids = Fk:currentRoom():getBanner("@$mcdh_MCGPile")
            local c = Fk:getCardById(to_select)
            if table.contains(ids, to_select) then
                return (Fk.currentResponsePattern == nil and player:canUse(c,{bypass_times = true})) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(c))
            end
        end
    end,
    before_use = function (self, player, use)
        local room = player.room
        local cards = Card:getIdList(use.card)
        use.extraUse = true
        room.tag["mcdh_mcg"] = room.tag["mcdh_mcg"] or {}
        for _, id in ipairs(cards) do
            table.removeOne(room.tag["mcdh_mcg"], id)
        end
        NotifyMCGPile(room)
    end,
    view_as = function (self, cards)
        if #cards ~= 1 then
            return nil
        end
        return Fk:getCardById(cards[1])
    end,
    enabled_at_play = function(self, player)
        return #Fk:currentRoom():getBanner("@$mcdh_MCGPile") >0
    end,
    enabled_at_response = function (self, player, response)
        local canuse = false
        local cards_id = Fk:currentRoom():getBanner("@$mcdh_MCGPile")
        for _, t in ipairs(cards_id) do
            local card = Fk:getCardById(t)
            if (Fk.currentResponsePattern == nil and player:canUse(card,{bypass_times = true})) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)) then
                canuse = true
                break
            end
        end

        return canuse
    end
})

chongfeng:addEffect('targetmod',{
    bypass_times = function(self, player, skill, scope, card)
        local room = Fk:currentRoom()
        return player:hasSkill(self) and table.contains(room:getBanner("@$mcdh_MCGPile"),card.id)
    end,
})

Fk:loadTranslationTable {
    ["mcdh_chongfeng"] = "冲锋",
    [":mcdh_chongfeng"] = "你可以无次数限制的使用或打出【磐蟹】下的一张牌。",
}

return chongfeng